// pages/user_end/order/order.js
const app = getApp();
const request = require('../../../utils/request.js');
const url = require('../../../utils/url.js');
const cookie = require('../../../utils/cookie.js');

Page({
  /**
   * 页面的初始数据
   */
  data: {
    bg: '#08B266',
    list1: [{
      name: '全部',
      dot: false,
      number: 0,
      checkinfo: 0
    }, {
      name: '已支付',
      dot: true,
      number: 0,
      checkinfo: 1
    }, {
      name: '未支付',
      dot: true,
      number: 0,
      checkinfo: 2
    }],
    nindex: 0,
    keyword:'',
    navindex:0,
    nsindex:0,
    page: 1,
    list: [],
    checkinfo: 0,
    show: false,
    show1: false,
    datelist: [],
    timelist: [],
    active: '',
    reserveDate: "",
    isReserve:0,
    orderId: 0,
    cnum:0,
    noOrder:0,
    login:0
  },
  close1(){
    this.setData({
      show1:false
    })
  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
     console.log(typeof(options.checkinfo))
     var that = this;
     if(typeof(options.checkinfo)=='string'){
       if(options.checkinfo=='0'){
          that.setData({
            navindex : 0,
            checkinfo: options.checkinfo,
            page :1,
          })
       }else{
         var navindex = parseInt(options.checkinfo)-1;
         that.setData({
          navindex :  options.checkinfo,
          checkinfo: options.checkinfo,
          page :1,
        })
       }
     }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var that = this;
    cookie.getCookie('userInfo', function (res) {
      if (res) {
        var etime = Date.parse(new Date()) / 1000;
        
        if (etime < res.expiretime) {
          that.setData({
            login: 1,
            token:res.token
          })
          // that.getDatelist();
          // that.getOrdernum();
          that.getOrder();
        }
      }else{
        that.setData({
          noOrder:1
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  reorder(e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../payorder/payorder?id=' + id
    })
  },
    doSearch(){
      this.setData({
        page:1
      })
      this.getOrdernum();
      this.getOrder();			
    },
    getOrdernum:function(){
      var that = this;
      let param = {
        scoure: 'wxapp',
        token:that.data.token,
        keyword:that.data.keyword
      }
      wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
      request.request(url.getordernum, param, url.REQUEST_METHOD_GET).then((res) => {
        wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
        if(res.data.code==1){
          var sitem = 'list1[0].number';
           that.setData({
              [sitem] :res.data.data.checkinfo
           })
           var sitem = 'list1[1].number';
           that.setData({
              [sitem] :res.data.data.checkinfo1
           })
           var sitem = 'list1[2].number';
           that.setData({
              [sitem] :res.data.data.checkinfo2
           })
           var sitem = 'list1[3].number';
           that.setData({
              [sitem] :res.data.data.checkinfo3
           })
           var sitem = 'list1[4].number';
           that.setData({
              [sitem] :res.data.data.checkinfo7
           })
           var sitem = 'list1[5].number';
           that.setData({
              [sitem] :res.data.data.checkinfo4
           })
           var sitem = 'list1[6].number';
           that.setData({
              [sitem] :res.data.data.checkinfo5
           })
         }
      })  	
    },
	
    getOrder: function() {
      var that = this;
      let param = {
          keyword:that.data.keyword,
		  checkinfo: that.data.checkinfo,
          page:that.data.page,
          token:that.data.token
      }
      wx.showNavigationBarLoading();  //在当前页面显示导航条加载动画
      request.request(url.getpxorderlist, param, url.REQUEST_METHOD_GET).then((res) => {
            wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
            if (that.data.page == 1) {
                that.setData({
                  list:[],
                  noOrder:1,
                })
            }
			
            var contentlistTem = that.data.list;
			console.log('xxxxxxx');
			console.log(res.data.data);
              if (res.data.data.length > 0) {
                var contentlist = res.data.data;
                if (contentlist.length < 10) {
                  that.setData({
                    list: contentlistTem.concat(contentlist),
                    hasMoreData: false,
                    noOrder:0,
                  })
                } else {
                  that.setData({
                    list: contentlistTem.concat(contentlist),
                    hasMoreData: true,
                    page: that.data.page + 1,
                    noOrder:0,
                  })
                }
              }else{
                 that.setData({
                  hasMoreData: false
                 })
              }
      })
  
    },
	chooseNav(e) {
	var current = e.currentTarget.dataset.current;
	  this.setData({
	    navindex : current,
	    checkinfo: e.currentTarget.dataset.checkinfo,
	    page :1,
	  })
	  if(this.data.login==1){
	     this.getOrder();
	  }
	},
    payOrder(e){
      var id = e.currentTarget.dataset.id;
      var that = this;
      wx.requestSubscribeMessage({
        tmplIds: [
          'WCOeBpOwto_yU4ZM-hUmQePvbfY_lxKyaFK5Yi7nzao',
        ],
        success: (res) => {
          that.confirmPayOrder(id);
        },
        fail: (res) => {
          that.confirmPayOrder(id);
        },
      })
    },
    confirmPayOrder(id){
      var that = this;
      let param = {
        scoure: 'wxapp',
        token:that.data.token,
        id:id
      }
      wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
      request.request(url.payorder, param, url.REQUEST_METHOD_GET).then((res) => {
        wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
        if(res.data.code==1){
            //直接支付成功
            //跳转订单详情页
           let orderId = res.data.data.id;
           if(res.data.data.pay_money>0){
             var config = JSON.parse(res.data.data.wxconfig);
             console.log("成功", JSON.parse(res.data.data.wxconfig));
             wx.requestPayment({
               'timeStamp': config.timestamp,
               'nonceStr':config.nonceStr,
               'package': config.package,
               'signType': config.signType,
               'paySign': config.paySign,
               'success': function(res2) {
                 console.log("成功", res2)
                 if (res2.errMsg == 'requestPayment:ok') {
                    wx.navigateTo({
                        url: '../order_choose/order_choose?id='+orderId,
                      })
                      return false;
                 } 
               },
               'fail': function(res2) {
                 console.log("失败", res2)
                 wx.navigateTo({
                  url: '../order_choose/order_choose?id='+orderId,
                 })
                 return false;
               },
               'complete': function(res2) {}
             })
           }else{
             wx.showToast({
               title: '支付成功',
               icon:'none'
             })
             wx.navigateTo({
               url: '../order_choose/order_choose?id='+orderId,
              })
              return false;      
           }
        }else{
          wx.showToast({
            title: res.data.msg,
            icon:'none',
            duration:2000
          })
        }
      })  	      
    },
    payconfig(e){
      var id = e.currentTarget.dataset.id;
      var that = this;
      let param = {
        scoure: 'wxapp',
        token:that.data.token,
        id:id
      }
      wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
      request.request(url.payconfig, param, url.REQUEST_METHOD_GET).then((res) => {
        wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
        if(res.data.code==1){
            //直接支付成功
            //跳转订单详情页
           let orderId = res.data.data.id;
           if(res.data.data.pay_money>0){
             var config = JSON.parse(res.data.data.wxconfig);
             console.log("成功", JSON.parse(res.data.data.wxconfig));
             wx.requestPayment({
               'timeStamp': config.timestamp,
               'nonceStr':config.nonceStr,
               'package': config.package,
               'signType': config.signType,
               'paySign': config.paySign,
               'success': function(res2) {
                 console.log("成功", res2)
                 if (res2.errMsg == 'requestPayment:ok') {
                      wx.navigateTo({
                        url: '../order_choose/order_choose?id='+orderId,
                      })
                      return false;
                 } 
               },
               'fail': function(res2) {
                 console.log("失败", res2)
                 wx.navigateTo({
                  url: '../order_choose/order_choose?id='+orderId,
                 })
                 return false;
               },
               'complete': function(res2) {}
             })
           }else{
             wx.showToast({
               title: '支付成功',
               icon:'none'
             })
             wx.navigateTo({
               url: '../order_choose/order_choose?id='+orderId,
              })
              return false;      
           }
        }else{
          wx.showToast({
            title: res.data.msg,
            icon:'none',
            duration:2000
          })
        }
      })  	      
    },
    changekey(e){
      var keyword =e.detail.value;
      this.setData({
        keyword:keyword
      })
    }


})